import java.util.*;
import java.io.*;

public class test {
    // 1.宝塔的高度为(1~n)
    // 2.每一行/列,不能出现高度相同的宝塔
    // 3.箭头:从该位置看,可以看到的宝塔个数
    public static int[] up;
    public static int[] down;
    public static int[] left;
    public static int[] right;
    public static int[][] nums;
    public static HashSet[] xx;
    public static HashSet[] yy;
    public static void main(String[] args) throws IOException {
        up = new int[]{0, 2, 2, 1, 3, 3};
        down = new int[]{0, 1, 4, 2, 2, 3};
        left = new int[]{0, 2, 2, 3, 2, 1};
        right = new int[]{0, 3, 3, 1, 2, 4};
        // 记录x,y轴的元素值
        xx = new HashSet[6];
        yy = new HashSet[6];
        for (int i = 0; i <= 5; i++) {
            xx[i] = new HashSet();
            yy[i] = new HashSet();
        }
        // 初始化nums的元素都是0,0代表未赋值
        nums = new int[6][6];
        dfs(1);
    }
    public static void dfs(int index) {
        if (index == 26) {
            // 只在完全填满时才检查所有条件
            if (checkAll()) {
                for (int i = 1; i <= 5; i++) {
                    for (int j = 1; j <= 5; j++) {
                        System.out.print(nums[i][j]);
                    }
                }
                System.exit(0);
            }
            return;
        }
        int[] t = getIndex(index);
        int x = t[0], y = t[1];
        for (int i = 1; i <= 5; i++) {
            if (!xx[x].contains(i) && !yy[y].contains(i)) {
                nums[x][y] = i;
                xx[x].add(i);
                yy[y].add(i);
                // 只在当前行/列填满时检查箭头
                if (isRowComplete(x) && !checkRow(x)) {
                    // 回溯
                    nums[x][y] = 0;
                    xx[x].remove(i);
                    yy[y].remove(i);
                    continue;
                }
                if (isColComplete(y) && !checkCol(y)) {
                    // 回溯
                    nums[x][y] = 0;
                    xx[x].remove(i);
                    yy[y].remove(i);
                    continue;
                }
                dfs(index + 1);
                // 回溯
                nums[x][y] = 0;
                xx[x].remove(i);
                yy[y].remove(i);
            }
        }
    }
    // 新增方法：检查行是否已填满
    public static boolean isRowComplete(int row) {
        return xx[row].size() == 5;
    }
    // 新增方法：检查列是否已填满
    public static boolean isColComplete(int col) {
        return yy[col].size() == 5;
    }
    // 新增方法：完整检查所有条件
    public static boolean checkAll() {
        for (int i = 1; i <= 5; i++) {
            if (!checkRow(i) || !checkCol(i)) {
                return false;
            }
        }
        return true;
    }
    // 分离出行检查逻辑
    public static boolean checkRow(int row) {
        int leftCount = 0, rightCount = 0;
        int leftMax = 0, rightMax = 0;
        // 左箭头检查
        for (int i = 1; i <= 5; i++) {
            if (nums[row][i] > leftMax) {
                leftCount++;
                leftMax = nums[row][i];
            }
        }
        // 右箭头检查
        for (int i = 5; i >= 1; i--) {
            if (nums[row][i] > rightMax) {
                rightCount++;
                rightMax = nums[row][i];
            }
        }
        return leftCount == left[row] && rightCount == right[row];
    }
    // 分离出列检查逻辑
    public static boolean checkCol(int col) {
        int upCount = 0, downCount = 0;
        int upMax = 0, downMax = 0;
        // 上箭头检查
        for (int i = 1; i <= 5; i++) {
            if (nums[i][col] > upMax) {
                upCount++;
                upMax = nums[i][col];
            }
        }
        // 下箭头检查
        for (int i = 5; i >= 1; i--) {
            if (nums[i][col] > downMax) {
                downCount++;
                downMax = nums[i][col];
            }
        }
        return upCount == up[col] && downCount == down[col];
    }
    public static int[] getIndex(int num) {
        // 13 -> [3,3]
        int x = (num - 1) / 5 + 1;
        int y = (num - 1) % 5 + 1;
        return new int[]{x, y};
    }
    class Read {
        StringTokenizer st = new StringTokenizer("");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String next() throws IOException {
            while (!st.hasMoreTokens()) {
                st = new StringTokenizer(br.readLine());
            }
            return st.nextToken();
        }
        Integer nextInt() throws IOException {
            return Integer.parseInt(next());
        }
        Long nextLong() throws IOException {
            return Long.parseLong(next());
        }
        Double nextDouble() throws IOException {
            return Double.parseDouble(next());
        }
    }
}